package com.android.lovely.room.chat.database

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@Entity(tableName = "CacheMessage")
@JsonClass(generateAdapter = true)
data class CacheMessage(
    @PrimaryKey(autoGenerate = false) @ColumnInfo(name = "id") @Json(name = "id") val id: Int = 0,
    @ColumnInfo(name = "card_id") @Json(name = "card_id") var cardId: Int = 0,
    @ColumnInfo(name = "bot_id") @Json(name = "bot_id") var botId: Int = 0,
    @ColumnInfo(name = "question") @Json(name = "question") var question: String = "",
    @ColumnInfo(name = "reply") @Json(name = "reply") var reply1: String = "",
    @ColumnInfo(name = "type") @Json(name = "type") var type: Int = 0,
    @ColumnInfo(name = "unlock") @Json(name = "unlock") var unlock: Boolean = false,
    @ColumnInfo(name = "point") @Json(name = "point") var point: Int = 0,
    @ColumnInfo(name = "intimacy_value") @Json(name = "intimacy_value") var intimacy: Int = 0,
    @ColumnInfo(name = "surplus") @Json(name = "surplus") var surplus: Int = 0,
) {

    // 使用枚举替换硬编码的类型值
    val messageType: MessageType
        get() = when (type) {
            MessageType.TEXT.value -> MessageType.TEXT
            MessageType.IMAGE.value -> MessageType.IMAGE
            else -> MessageType.TEXT
        }

    // 将 send 属性改为方法
    fun getSendMessage(): BaseMessage? {
        return when {
            question.isNotEmpty() -> when (messageType) {
                MessageType.TEXT -> SendTextMessage(id, question)
                MessageType.IMAGE -> ImageMessage(id, cardId, question, unlock, point, intimacy)
            }
            else -> null
        }
    }

    // 将 reply 属性改为方法
    fun getReplyMessage(): BaseMessage? {
        return when {
            reply1.isNotEmpty() -> when (messageType) {
                MessageType.TEXT -> ReceivedTextMessage(id, reply1)
                MessageType.IMAGE -> ImageMessage(id, cardId, reply1, unlock, point, intimacy)
            }
            else -> null
        }
    }
}

// 定义消息类型枚举，提高代码可读性
enum public class MessageType(val value: Int) {
    TEXT(0),
    IMAGE(1)
}
